'''
Case Type： 主备重建异常
Case Name： 受存储空间影响，导致主备重建异常，进行备机重建
Create At:  2020/07/24
Owner    :  @li-xin12345
Descption:  1.查看磁盘剩余大小 2.构造大文件使备机磁盘被占满
            3.进行备机重建 4.删除大文件恢复磁盘 5.重新进行备机重建
'''

import os
import sys
import unittest
import time
from yat.test import Node
from testcase.utils.CommonSH import *
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.CommonSH import *

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common

logger = Logger()


class IndexFileDamaged(unittest.TestCase):
    sh_primy = CommonSH('PrimaryDbUser')
    sh_stadb = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        logger.info('------------Opengauss_Reliability_Dbsys_Case220.py start------------')
        self.rootNode = Node('PrimaryRoot')
        self.primaryNode = Node(node='PrimaryDbUser')
        self.standbyNode = Node(node='Standby1DbUser')
        self.standbyRootNode = Node(node='Standby1Root')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.Constant = Constant()
        self.com = Common()

    def test_Index_file_damaged(self):
        # 备机大文件构造
        logger.info('----------------------构造大文件-----------------------')
        logger.info('Available:' + self.com.getAvailSize(self.standbyNode, macro.DB_INSTANCE_PATH))

        logger.info("-----------get standby disk name -----------")
        cmd = f"df -h {macro.DB_INSTANCE_PATH}"
        logger.info(cmd)
        msg = self.standbyRootNode.sh(cmd).result()
        logger.info(msg)
        disk_name = msg.splitlines()[-1].split()[0].strip()

        logger.info("-----------standby disk full -----------")
        cfediskCmd = f'''{macro.CFE_PATH}/cfe  "inject  rfile_full (diskname)  values  ({disk_name})"'''
        logger.info(cfediskCmd)
        tmp = self.standbyRootNode.sh(cfediskCmd).result()
        logger.info(tmp)
        logger.info('Available:' + self.com.getAvailSize(self.standbyNode, macro.DB_INSTANCE_PATH))

        # 进行备机重建失败，提示“No space left on device”
        logger.info('------------------进行备机重建-----------------------------------')
        buildMsg = self.sh_stadb.executeGsctl('build', self.Constant.NO_SPACE_BUILD_FAIL_MSG, '-b full')
        logger.info(buildMsg)
        self.assertTrue(buildMsg)

        # 备机磁盘恢复
        logger.info('-----------------删除大文件进行磁盘恢复----------------------------------')
        logger.info("-----------clean disk-----------")
        cfediskCmd = f'''{macro.CFE_PATH}/cfe "clean  rfile_full  where(diskname={disk_name})"'''
        logger.info(cfediskCmd)
        tmp = self.standbyRootNode.sh(cfediskCmd).result()
        logger.info(tmp)
        logger.info('Available:' + self.com.getAvailSize(self.standbyNode, macro.DB_INSTANCE_PATH))

        # 重建前打印剩余空间信息
        logger.info('------------------重建前打印剩余空间信息-----------------------------------')
        logger.info('Available:' + self.com.getAvailSize(self.standbyNode, macro.DB_INSTANCE_PATH))

        # 恢复后第一次重建可能失败，需要再次重建
        logger.info('------------------重新进行备机重建-----------------------------------')
        buildMsg = self.sh_stadb.executeGsctl('build', self.Constant.REBUILD_SUCCESS_MSG, '-b full')
        logger.info(buildMsg)
        self.assertTrue(buildMsg)

    def tearDown(self):
        self.Constant = Constant()
        logger.info('----------------------------后置处理-----------------------------')
        try:
            db_status = self.sh_primy.getDbInstanceStatus()
            logger.info(db_status)
            if db_status:
                pass
            else:
                is_started = self.sh_primy.startDbCluster()
                self.assertTrue(is_started)
                logger.info(f'{is_started}')
                logger.info(f'db_status: {is_started}')
        finally:
            self.copy_path = macro.DB_INSTANCE_PATH.split('cluster')[0]
            rm_bigfile = f'rm {self.copy_path}test'
            logger.info(rm_bigfile)
            self.standbyNode.sh(rm_bigfile)
            logger.info('------------------重新进行备机重建-----------------------------------')
            buildMsg = self.sh_stadb.executeGsctl('build', self.Constant.REBUILD_SUCCESS_MSG, '-b full')
            logger.info(buildMsg)
        logger.info('----------------------------Reliability_Dbsys_Case220执行完成-----------------------------')
